package message.utils;

public class StringUtils {
    /**
     * 右补充
     *
     * @param text         原字符串
     * @param targetLength 目标长度
     * @param ch           补充字符
     * @param encoding     编码
     * @return
     */
    public static String rightFill(String text, int targetLength, char ch, String encoding) {

        String repeatedChars = repeat(ch, targetLength);
        return text.concat(repeatedChars);
    }

    /**
     * 左补充
     *
     * @param text         原字符串
     * @param targetLength 目标长度
     * @param ch           补充字符
     * @param encoding     编码
     * @return
     */
    public static String leftFill(String text, int targetLength, char ch, String encoding) {
        String repeatedChars = repeat(ch, targetLength);
        return repeatedChars.concat(text);
    }

    /**
     * 产生重复字符的字符串
     *
     * @param ch
     * @param repeat
     * @return
     */
    public static String repeat(final char ch, final int repeat) {
        final char[] buf = new char[repeat];
        for (int i = repeat - 1; i >= 0; i--) {
            buf[i] = ch;
        }
        return new String(buf);
    }

    public static byte[] fmtFill(byte[] bInt, String params) {
        int fdot = params.indexOf(",");
        int bdot = params.lastIndexOf(",");
        if (params.charAt(fdot + 1) == ',') {
            ++fdot;
        }

        String fillChar = params.substring(0, fdot);
        byte s = convertStringToChar(fillChar);
        int num = Integer.parseInt(params.substring(fdot + 1, bdot));
        String direct = params.substring(bdot + 1);
        int len = bInt.length;
        int j = num - len;
        if (j <= 0) {
            return bInt;
        } else {
            byte[] bt = new byte[j];
            byte[] rpbt = new byte[len + j];

            for(int i = 0; i < j; ++i) {
                bt[i] = s;
            }

            if (direct.equals("LEFT")) {
                System.arraycopy(bt, 0, rpbt, 0, j);
                System.arraycopy(bInt, 0, rpbt, j, len);
                return rpbt;
            } else if (direct.equals("RIGHT")) {
                System.arraycopy(bInt, 0, rpbt, 0, len);
                System.arraycopy(bt, 0, rpbt, len, j);
                return rpbt;
            } else {
                return bInt;
            }
        }
    }

    public static byte convertStringToChar(String str) {
        if (str.length() == 1) {
            return (byte)str.charAt(0);
        } else if (str.equals("\\n")) {
            return 10;
        } else if (str.equals("\\t")) {
            return 9;
        } else if (str.equals("\\r")) {
            return 13;
        } else if (str.equals("\\f")) {
            return 12;
        } else if (str.equals("\\")) {
            return 92;
        } else if (str.equals("\\,")) {
            return 44;
        } else if (str.length() == 4 && str.substring(0, 2).equalsIgnoreCase("0x")) {
            return (byte)Integer.parseInt(str.substring(2), 16);
        } else {
            System.err.println("不支持该填充字符！[" + str + "]");
            return 0;
        }
    }
}
